<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>6_2_1-1-对Prop属性进行验证</title>
</head>

<body>


  <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
  <div id="Application">
    <comp1 :count="7"></comp1>
  </div>
  <script>
    const App = Vue.createApp({});
    const comp1 = {
      props: {
        count: {
          //定义此属性的类型为数值类型
          type: Number,
          //设置此属性是否必传
          required: false,
          //设置默认值
          //default: 10,
          //函数执行结果为当前默认值（默认值获取方式复杂情况下）
          default: function () {
            return 10;
          },
          //自定义验证，count值大于10
          validator: function (value) {
            if (typeof value != "number" || value <= 10) {
              return false;
            }
            return true;
          },
        },
      },
      data() {
        return {
          thisCount: 0,
        };
      },
      methods: {
        click() {
          this.thisCount += 1;
        },
      },
      computed: {
        innerCount: {
          get() {
            return this.count + this.thisCount;
          },
        },
      },
      template: `
        <button @click="click">点击</button>
        <div>计数：{{innerCount}}</div>
        `,
    };
    App.component("comp1", comp1);
    App.mount("#Application");
  </script>
</body>

</html>